package com.shelpe.services.scheduler.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shelpe.services.scheduler.dto.BaseDto;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.po.Users;

public class Util {
	
	public static long processThreadNum(){
		
		int cpuNum = Runtime.getRuntime().availableProcessors();
		int nThreadNum = cpuNum > 0 ? cpuNum : 1;
		
		return nThreadNum * 2 + 1;
	}
	
	public static long computePageSize(long total, long pages){
		
		if(total == 0 || pages == 0){
			return 0;
		}
		
		long pageSize = total / pages;
		
		pageSize = (total % pages > 0) ? (pageSize + 1) : pageSize;
		
		return pageSize;
	}
	
	public static SyncLog convert(BaseDto item){

		SyncLog log = new SyncLog();
		if(item == null){
			return log;
		}

		log.setUserId(item.getUserId());
		log.setNick(item.getNick());
		log.setProcess(item.getProcess());
		log.setAddNum(item.getAddNum());
		log.setUpdateNum(item.getUpdateNum());
		log.setDeleteNum(item.getDeleteNum());
		log.setMessage(item.getMessage());
		log.setDurTime(item.getDurTime());
		log.setSyncDate(new Date());
	
		return log;
	}
	
	public static void buildExceptionSyncLog(SyncLog log, Long userId, String process, Throwable t){
		
		if(log != null){
			log.setUserId(userId);
			log.setProcess(process);
			log.setSyncDate(new Date());
			log.setState(2);
			log.setMessage("unknown error");
			if(t.getCause() != null){
				String message = t.getCause().getMessage();
				if(message != null && message.contains("Api Request Error")){
					log.setMessage("淘宝Api调用失败");
				}
			}
		}
	}

	public static List<SyncLog> convert(List<? extends BaseDto> items){
		
		List<SyncLog> logs = new ArrayList<SyncLog>();
		
		if(items == null){
			return logs;
		}
		
		for(BaseDto dto : items){
			SyncLog log = new SyncLog();
			log.setUserId(dto.getUserId());
			log.setNick(dto.getNick());
			log.setProcess(dto.getProcess());
			log.setAddNum(dto.getAddNum());
			log.setUpdateNum(dto.getUpdateNum());
			log.setDeleteNum(dto.getDeleteNum());
			log.setMessage(dto.getMessage());
			log.setDurTime(dto.getDurTime());
		}
		
		return logs;
	}
	
	public static Map<String, String> getTaskRange(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dateNow = new Date();
		String dateNowStr = sdf.format(dateNow);
		Date dateNowFormat = new Date();;
		try {
			dateNowFormat = sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Calendar calendar = Calendar.getInstance(); 
		
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24*3);//三天的数据
		Date startDate = calendar.getTime();
		
		calendar.setTime(dateNowFormat); 
		calendar.add(Calendar.HOUR_OF_DAY,-24);//减一天
		Date endDate = calendar.getTime();
		
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		
		return map;
	}
	
	public static List<Users> getSubTaskList(List<Users> list, Long beginPageNo, Long subPages){
		
		if(list == null){
			return null;
		}
		
		int fromIndex = 0;
		int toIndex = list.size();
		
		if(beginPageNo != null && subPages != null){
			
			int pages = subPages.intValue();
			if(pages <= 0){
				pages = 1;
			}
			
			int pageNo = beginPageNo.intValue();
			if(pageNo < 0){
				pageNo = 0;
			}
			
			int pageSize = list.size() / pages;
			if(list.size() % pages > 0){
				pageSize++;
			}
			
			fromIndex = pageNo * pageSize;
			int toIndexTemp = (pageNo + 1) * pageSize;
			toIndex = toIndexTemp > list.size() ? list.size() : toIndexTemp;
		}
		
		return fromIndex < toIndex ? list.subList(fromIndex, toIndex) : null;
	}
	
	public static List<Map<String, String> > splitTaskRange(String startDate, String endDate){
		
		List<Map<String, String> > res = new ArrayList<Map<String, String> >();
		
		if(startDate == null || endDate == null){
			return res;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date startDateFormat = new Date();
		Date endDateFormat = new Date();
		
		try {
			startDateFormat = sdf.parse(startDate);
			endDateFormat = sdf.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Calendar calendar = Calendar.getInstance(); 
		Date endDateTemp = endDateFormat;
		
		while(startDateFormat.getTime() <= endDateFormat.getTime()){//endDateFormat should not been changed
			
			Map<String, String> map = new HashMap<String, String>();
			map.put("startDate", sdf.format(startDateFormat));
			
			calendar.setTime(startDateFormat);
			calendar.add(Calendar.HOUR_OF_DAY, 24*29);//add 29 day
			endDateTemp = calendar.getTime();
			
			if(endDateFormat.getTime() < endDateTemp.getTime()){//限制时间范围
				endDateTemp = endDateFormat;
			}
			
			map.put("endDate", sdf.format(endDateTemp));
			
			res.add(map);
			
			calendar.setTime(endDateTemp);
			calendar.add(Calendar.HOUR_OF_DAY, 24);//add one day
			startDateFormat = calendar.getTime();
		}
		
		return res;
	}

}
